using Juli.Mes.Mqtt;
using MQTTnet.AspNetCore;

namespace Juli.Mes
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();

        }

        private static IHostBuilder CreateHostBuilder(string[] args) =>

            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    //var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
                    //.AddJsonFile("host.json")
                    //.Build();

                    //var url = configuration["urls"];

                    //webBuilder.ConfigureKestrel((context, options) => { options.Limits.MaxRequestBodySize = 1024 * 50; });
                    //webBuilder.UseUrls(configuration["urls"]);
                    webBuilder.UseStartup<Startup>();

                    webBuilder.ConfigureKestrel((context, options) =>
                    {
                        options.Limits.MaxRequestBodySize = 1024 * 50;

                        var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
                            .AddJsonFile("appsettings.json")
                            .Build();

                        var mqttServerOptions = configuration.GetSection("IotCloudMqttServer").Get<IotCloudMqttServerOptions>();

                        // This will allow MQTT connections based on TCP port xxxx .
                        options.ListenAnyIP(port: mqttServerOptions.Port, opts => opts.UseMqtt());

                        // This will allow MQTT connections based on HTTP WebSockets with URI "localhost:xxxx/mqtt"
                        // See code below for URI configuration.
                       // options.ListenAnyIP(mqttServerOptions.WebSocketPort); // Default HTTP pipeline
                    });

                })
 
                .UseSerilog((context, loggerConfiguration) =>
                {
                    SerilogToEsExtensions.SetSerilogConfiguration(
                        loggerConfiguration,
                        context.Configuration);
                }).UseAutofac();

    }
}
